package org.ohuyo.libra.client.service.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.ohuyo.commons.servlet.ServletUtils;
import org.ohuyo.libra.base.message.Response;
import org.ohuyo.libra.base.message.impl.AppValidateReq;
import org.ohuyo.libra.base.message.impl.AppValidateResp;
import org.ohuyo.libra.base.message.impl.JumpValidateReq;
import org.ohuyo.libra.base.message.impl.JumpValidateResp;
import org.ohuyo.libra.base.message.service.ClientMessageService;
import org.ohuyo.libra.base.message.service.SlaveMessageService;
import org.ohuyo.libra.client.config.ClientConfig;
import org.ohuyo.libra.client.config.SlaveClientConfig;
import org.ohuyo.libra.client.define.ClientConst;
import org.ohuyo.libra.client.service.AbstractClientService;
import org.ohuyo.libra.client.service.JumpInfo;
import org.ohuyo.libra.client.service.SlaveClientService;
import org.ohuyo.libra.client.session.LibraSession;
import org.ohuyo.libra.client.session.LibraSessionUtils;
import org.ohuyo.libra.client.session.SlaveLibraSessionImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * �ӿͻ��˷���ʵ����
 * 
 * @author rabbit
 * 
 */
public class SlaveClientServiceImpl extends AbstractClientService implements
		SlaveClientService {
	/**
    *
    */
	private final Logger log = LoggerFactory
			.getLogger(SlaveClientServiceImpl.class);

	/**
	 * 
	 */
	private SlaveMessageService slaveMessageService;

	/**
	 * 
	 */
	private SlaveClientConfig slaveClientConfig;

	public SlaveMessageService getSlaveMessageService() {
		return slaveMessageService;
	}

	public void setSlaveMessageService(SlaveMessageService slaveMessageService) {
		this.slaveMessageService = slaveMessageService;
	}

	public SlaveClientConfig getSlaveClientConfig() {
		return slaveClientConfig;
	}

	public void setSlaveClientConfig(SlaveClientConfig slaveClientConfig) {
		this.slaveClientConfig = slaveClientConfig;
	}

	public boolean validateApp(HttpSession session) {
		LibraSession sess = LibraSessionUtils.getSession(session);
		return validateApp(sess);
	}

	public boolean validateApp(LibraSession sess) {
		AppValidateReq req = new AppValidateReq();
		fillRequset(req, sess);
		req.setLoginTicket(sess.getLoginTicket());
		req.setAppTicket(sess.getAppTicket());
		AppValidateResp res = slaveMessageService.validateApp(req);
		if (res.getResCode() == Response.RES_CODE_SUCCESS) {
			return true;
		} else {
			//�޸���־��¼ģʽ zhangzhaoxing 20120515
			log.debug("validate appTicket faild. " + "\nresCode="
					+ res.getResCode() + "\nresMsg=" + res.getResMsg()
					+ "\nloginTicket=" + sess.getLoginTicket() + "\nappTicket="
					+ sess.getAppTicket());
			return false;
		}
	}

	public JumpInfo validateJump(String loginTicket, String jumpTicket,
			String clientTicket, HttpServletRequest httpServletRequest) {
		String appCode = slaveClientConfig.getAppCode();
		JumpValidateReq req = new JumpValidateReq();
		fillRequset(req, appCode);
		req.setJumpTicket(jumpTicket);
		req.setLoginTicket(loginTicket);
		req.setClientTicket(clientTicket);
		JumpValidateResp res = slaveMessageService.validateJump(req);
		if (res.getResCode() == Response.RES_CODE_SUCCESS) {
			// ��֤�ɹ�,����libra session��Ϣ
			String id = res.getCustId();
			String idType = res.getCustIdType();
			String loginAddr = res.getLoginAddr();
			String appTicket = res.getAppTicket();
			String att = res.getAttachment();
			String remoteAddr = ServletUtils.getRemoteAddr(httpServletRequest);
			HttpSession session = httpServletRequest.getSession();

			SlaveLibraSessionImpl sess = new SlaveLibraSessionImpl();
			sess.setAppCode(appCode);
//			sess.setAppIp(httpServletRequest.getLocalAddr());
//			sess.setAppPort(httpServletRequest.getLocalPort());
			sess.setAppTicket(appTicket);
			sess.setAttachment(att);
			sess.setCustId(id);
			sess.setCustIdType(idType);
			sess.setLoginAddr(remoteAddr);
			sess.setLoginTicket(loginTicket);
			sess.setRemoteAddr(remoteAddr);
			sess.setSlaveClientService(this);

			sess.setJumpTicket(jumpTicket);
			sess.setClientTicket(clientTicket);
			
			if (sess instanceof LibraSession) {
				LibraSession libraSess = sess;
				try {
					sess.setLoginName(libraSess.getSharedAttribute("libraMasterClient", "loginName"));
					sess.setUserName(libraSess.getSharedAttribute("libraMasterClient", "userName"));
					sess.setMenuList(libraSess.getSharedAttribute("libraMasterClient", "permissions"));
					sess.setOrgList(libraSess.getSharedAttribute("libraMasterClient", "organization"));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
//			sess.

			session.setAttribute(ClientConst.LIBRA_SESSION, sess);
			JumpInfo ret = new JumpInfo(id, idType, loginAddr);
			return ret;
		}
		return null;
	}

	@Override
	protected ClientConfig getClientConfig() {
		return slaveClientConfig;
	}

	@Override
	protected ClientMessageService getClientMessageServcie() {
		return slaveMessageService;
	}

}
